package com.sx.pipeline.ui.goods

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.app.event.ProductEvent
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.EquipmentAddDTO
import com.sx.pipeline.repos.http.req.GoodsAddDTO
import com.sx.pipeline.repos.http.res.*
import com.sx.pipeline.utils.AndroidUtil

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class GoodsVM(app: Application) : CYBaseViewModel(app) {
    // 省ID
    val provinceIDField: ObservableField<Int> = ObservableField()

    // 市ID
    val cityIDField: ObservableField<Int> = ObservableField()

    // 区ID
    val areaIDField: ObservableField<Int> = ObservableField()

    // 地址详情
    val addressField: ObservableField<String> = ObservableField()

    // 经度
    val longitudeField: ObservableField<String> = ObservableField()

    // 纬度
    val latitudeField: ObservableField<String> = ObservableField()
    /**
     * 获取产品配置数据
     */
    //销售分类
    val saleClassData: MutableList<String> = ArrayList()
    fun getProduct() {
        addSubscribe(
            mModel.getProduct()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<TaskConfigVo, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: TaskConfigVo) {
                        for (data in result.SALES_CLASS) {
                            saleClassData?.add(data.name)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }


    /**
     * 添加商品
     */
    fun productAdd(
        name:String, //商品名称
        productModel:String,//产品型号
        parameter:String, //主要参数
        manufacturer:String,//制造厂家
        price:String, //价格
        addressDetail:String,
        unit:String, //商品销售单位
        contact:String, //联系人
        contactPhone:String,//联系电话
        salesClass:String, //销售分类
        productImage:List<String>,//设备图片
    ) {

        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请填写商品名称")
            return
        }
        if (TextUtils.isEmpty(productModel)) {
            ToastUtils.showShort("请填写商品型号")
            return
        }
        if (TextUtils.isEmpty(parameter)) {
            ToastUtils.showShort("请填写主要参数")
            return
        }
        if (TextUtils.isEmpty(manufacturer)) {
            ToastUtils.showShort("请填写生产厂家")
            return
        }


        if (TextUtils.isEmpty(price)) {
            ToastUtils.showShort("请填写价格")
            return
        }

        if (TextUtils.isEmpty(addressDetail)) {
            ToastUtils.showShort("请选择服务区域")
            return
        }

        if (TextUtils.isEmpty(unit)) {
            ToastUtils.showShort("请填写商品销售单位")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(salesClass)) {
            ToastUtils.showShort("请填写商品销售分类")
            return
        }
        if (productImage.size==0) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        val dto = GoodsAddDTO(
            0, name, productModel, parameter, manufacturer, price,
            provinceIDField.get()!!,
            cityIDField.get()!!,
            areaIDField.get()!!, longitudeField.get().toString(), latitudeField.get().toString(), addressDetail, unit,contact, contactPhone, salesClass,
            AndroidUtil.listToString(productImage).toString()
        )
        addSubscribe(
            mModel.productAdd(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("商品添加完成")
                        //通知列表刷新
                        RxBus.getDefault().post(ProductEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }

    /**
     * 编辑商品
     */
    fun productEdit(
        id: Int,
        name:String, //商品名称
        productModel:String,//产品型号
        parameter:String, //主要参数
        manufacturer:String,//制造厂家
        price:String, //价格
        addressDetail:String,
        unit:String, //商品销售单位
        contact:String, //联系人
        contactPhone:String,//联系电话
        salesClass:String, //销售分类
        productImage:List<String>,//设备图片
    ) {

        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请填写商品名称")
            return
        }
        if (TextUtils.isEmpty(productModel)) {
            ToastUtils.showShort("请填写商品型号")
            return
        }
        if (TextUtils.isEmpty(parameter)) {
            ToastUtils.showShort("请填写主要参数")
            return
        }
        if (TextUtils.isEmpty(manufacturer)) {
            ToastUtils.showShort("请填写生产厂家")
            return
        }


        if (TextUtils.isEmpty(price)) {
            ToastUtils.showShort("请填写价格")
            return
        }

        if (TextUtils.isEmpty(addressDetail)) {
            ToastUtils.showShort("请选择服务区域")
            return
        }

        if (TextUtils.isEmpty(unit)) {
            ToastUtils.showShort("请填写商品销售单位")
            return
        }
        if (TextUtils.isEmpty(contact)) {
            ToastUtils.showShort("请填写联系人")
            return
        }
        if (TextUtils.isEmpty(contactPhone)) {
            ToastUtils.showShort("请填写联系电话")
            return
        }
        if (TextUtils.isEmpty(salesClass)) {
            ToastUtils.showShort("请填写商品销售分类")
            return
        }
        if (productImage.size==0) {
            ToastUtils.showShort("请上传设备图片")
            return
        }
        val dto = GoodsAddDTO(
            id, name, productModel, parameter, manufacturer, price,
            provinceIDField.get()!!,
            cityIDField.get()!!,
            areaIDField.get()!!, longitudeField.get().toString(), latitudeField.get().toString(), addressDetail, unit,contact, contactPhone, salesClass,
            AndroidUtil.listToString(productImage).toString()
        )
        addSubscribe(
            mModel.productEdit(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {

                    override fun onResult(result: Any) {
                        ToastUtils.showShort("商品编辑完成")
                        //通知列表刷新
                        RxBus.getDefault().post(ProductEvent())
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort("$msg")

                    }
                })
        )
    }
    /**
     * 获取商品列表
     */
    var page = 1
    val deviceListEvent: SingleLiveEvent<List<GoodsInfoVo>> = SingleLiveEvent()
    fun getProductList(key: String) {
        addSubscribe(
            mModel.getProductList(key, page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<GoodsInfoVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<GoodsInfoVo>) {
                        deviceListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取商品详情
     */
    val goodsDetailDataEvent: SingleLiveEvent<GoodsInfoVo> = SingleLiveEvent()
    fun getGoodsDetail(id: Int) {
        addSubscribe(
            mModel.getGoodsDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<GoodsInfoVo, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: GoodsInfoVo) {
                        goodsDetailDataEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 删除台账设备
     */
    fun deleteProduct(id: Int) {
        addSubscribe(
            mModel.deleteProduct(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("商品删除完成")
                        //通知列表刷新
                        RxBus.getDefault().post(ProductEvent())
                        finish()
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}